package com.servlet;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.bean.User;
import com.daofactory.DaoFactory;

/**
 * @author 徐洲
 * @version 1.0
 * 用户登录
 */
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        if ("login".equals(action)) {
            String enteredCaptcha = request.getParameter("captcha");
            String storedCaptcha = (String) session.getAttribute("captcha");
            //验证码
            if (enteredCaptcha != null && enteredCaptcha.equals(storedCaptcha)) {
                //用户登录
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                int userid = DaoFactory.getUserDAOInstance().userLogin(username, password);
                if (userid > 0) {
                    // 登录成功
                    session.setAttribute("userid", userid);
                    session.setAttribute("username", username);
                    response.sendRedirect("./index.jsp");
                    return;
                } else {

//                    // 登录失败
                    session.setAttribute("info", "输入的用户名或密码不正确!");
                }
            } else {
                // 验证码错误
                session.setAttribute("info", "验证码错误，请重新输入!");
//                request.setAttribute("codefail","验证码错误，请重新输入");
//                request.getRequestDispatcher("login.jsp").forward(request,response);
            }
        } else if ("register".equals(action)) {
            //注册用户
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String telephone = request.getParameter("email");
            if (DaoFactory.getUserDAOInstance().userRegister(username, password, telephone)) {
                session.setAttribute("info", "注册成功！");
            } else {
                session.setAttribute("info", "注册失败！请重新注册！");
            }
        } else if ("changePassword".equals(action)) {
            String username = request.getParameter("username");
            String newPwd = request.getParameter("newPwd");
            String oldPwd = request.getParameter("oldPwd");
            if (DaoFactory.getUserDAOInstance().userIfPwd(oldPwd, username)) {
                if (DaoFactory.getUserDAOInstance().userChangePassword(newPwd, username)) {
                    session.setAttribute("info", "密码修改成功");
                } else {
                    session.setAttribute("info", "密码修改失败");
                }
            } else {
                session.setAttribute("info", "原密码不正确");
            }
        } else if ("changeInfo".equals(action)) {
            User user = new User();
            String sex = request.getParameter("sex");
            String telephone = request.getParameter("telephone");
            String email = request.getParameter("email");
            String username = request.getParameter("username");
            user.setSex(sex);
            user.setTelephone(telephone);
            user.setEmail(email);
            user.setUsername(username);

            if (DaoFactory.getUserDAOInstance().userChangeInfo(user)) {
                session.setAttribute("info", "修改个人信息成功");
            } else {
                session.setAttribute("info", "修改个人信息失败");
            }
        }
        response.sendRedirect("./infoPage.jsp");
    }
}
